<?php
class Cl_Category extends WdkClass 
{
	protected
		/******************************************************/
		//	Original data - 
		/******************************************************/
		$category_seq			= 0		,	//	
		$category_name			= ''	,	//	
		$category_code			= 0		,	//	
		$category_private_flag	= 'F'	,	//	
		/******************************************************/
		//	Additional data
		/******************************************************/
		$parent_code			= ''	;	//	
	
	/**
	 * 	
	 */
	function __construct()
	{
		parent::__construct();
		self::getDbo();
	}
	
	/**
	 *	
	 */
	public function __init($data)
	{
		parent::__init($data);
		self::dformat();
	}
	
	/**
	 *	
	 */
	public function load($category_seq)
	{
		/*------------------------------------------------------------*/
		$dbo = self::getDbo();
		/*------------------------------------------------------------*/
		self::__init($dbo->cLoad($category_seq));
	}
	
	/**
	 *	
	 */
	public function loadByCode($category_code)
	{
		/*------------------------------------------------------------*/
		$dbo = self::getDbo();
		/*------------------------------------------------------------*/
		self::__init($dbo->cLoadByCode($category_code));
	}
	
	/**
	 *	
	 */
	public function dformat()
	{
		$this->parent_code = substr($this->category_code, 0 , strlen($this->category_code) - 2);
	}
	
	/**
	 * 
	 */
	public function autoUpdate($log='')
	{
		if((int)$this->category_seq > 0)
		{
			$this->update($log);
		}
		else
		{
			$this->insert($log);
		}
	}
	
	/**
	 *	
	 */
	public function insert($log='Insert')
	{
		/*------------------------------------------------------------*/
		$dbo = self::getDbo();
		/*------------------------------------------------------------*/
		
		$this->category_code = $dbo->nextCode($this->parent_code);
		
		$data = array(	'category_name'			=>	$this->category_name,
						'category_code'			=>	$this->category_code,
						'category_private_flag'	=>	$this->category_private_flag
					);
		$this->category_seq = $dbo->insert($data);
		/******************************************************/
		parent::setBackupData(array_keys($data));
		$this->backup($log);
		/******************************************************/
	}
	
	/**
	 * 
	 */
	public function update($log='update')
	{
		/*------------------------------------------------------------*/
		$dbo = self::getDbo();
		/*------------------------------------------------------------*/
		if(parent::isUpdatable())
		{
			$data = array(	'category_name'			=>	$this->category_name,
							'category_code'			=>	$this->category_code,
							'category_private_flag'	=>	$this->category_private_flag
						);
			$dbo->update($data, array("category_seq = ".$this->category_seq));
			/******************************************************/
			parent::setBackupData(array_keys($data));
			$this->backup($log);
			/******************************************************/
		}
	}
	
	/**
	 * 
	 */
	public function delete($log='delete')
	{
		/*------------------------------------------------------------*/
		$dbo = self::getDbo();
		/*------------------------------------------------------------*/
		$dbo->pdelete(array("category_seq = ".$this->category_seq));
		/******************************************************/
		parent::setBackupData();
		$this->backup($log);
		/******************************************************/
	}
	
	/**
	 * 
	 */
	public function Move($prev_category_code='')
	{
		/*------------------------------------------------------------*/
		$dbo = self::getDbo();
		$cate = Wdk::helper('Category');
		/*------------------------------------------------------------*/
		$current_code = $this->category_code;
		$target_code = substr($prev_category_code, 0, -2).$cate->next(substr($prev_category_code, -2));
		
		//	Change in the same depth
		if(strlen($current_code) == strlen($target_code))
		{
			$dbo->makeTemp($current_code);
			//	Case 1. move up in same depth
			if($current_code > $target_code)
			{
				$dbo->moveUp($target_code, $current_code);
			}
			//	Case 2. move down in same depth
			else
			{
				$dbo->moveDown($current_code, $target_code);
			}
			$dbo->releaseTemp($target_code);
		}
		//	Case 3. move into different depth
		{
			
		}
		
		$this->update("Move");
	}
	
	/**
	 *	
	 */
	private function backup($log='')
	{
		/*------------------------------------------------------------*/
		$dbo = self::getDbo();
		/*------------------------------------------------------------*/
		$dbo->log($this->category_seq, $log, $this->backup_data);
	}
	
	/**
	 * 
	 */
	public function getDbo()
	{
		return Wdk::model(substr(__CLASS__, 3));
	}
}